<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>新闻列表</title>
    <style>
        .news-box {
            width: 600px;
            margin: auto;
        }

        .news-item {
            display: flex;
            height: 120px;
            padding: 20px 0;
            cursor: pointer;
        }

        .news-item-left {
            flex: 1;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            padding-right: 10px;
        }

        .news-item-left .title {
            font-size: 20px;
        }

        .news-item-left .info {
            color: #999999;
        }

        .news-item-left .info span {
            margin-right: 20px;
        }

        .news-item-right {
            width: 160px;
            height: 120px;
        }

        .news-item-right img {
            width: 100%;
            height: 100%;
        }

        .loading:before{ content:""; position: absolute; left: 0; top: 0; width: 100%; height: 100%; background: #fff url(./images/loading.gif) no-repeat center;}
    </style>
</head>

<body>
    <div id="app">
        <div class="news-box" v-loading="isLoading">
            <div class="news-item" v-for="item in newsList" :key="item.id">
                <!-- 左边 -->
                <div class="news-item-left">
                    <div class="title">{{ item.title }}</div>
                    <div class="info">
                        <span>{{ item.source }}</span>
                        <span>{{ item.time }}</span>
                    </div>
                </div>
                <!-- 右边 -->
                <div class="news-item-right">
                    <img :src="item.img" alt="">
                </div>
            </div>
        </div>
    </div>
    <script src="./js/vue@3.5.13.js"></script>
    <script src="./js/axios.js"></script>
    <script>
        const { createApp, onMounted, ref } = Vue
        const app = createApp({
            setup(){
                // 定义一个新闻列表
                let newsList = ref([])
                // 定义一个loading状态
                let isLoading = ref(true)

                // 生命周期函数：请求数据
                onMounted( async () => {
                    let res = await axios.get("http://hmajax.itheima.net/api/news")
                    console.log(res.data.data);
                    // 将请求到的数据赋值给 新闻列表
                    newsList.value = res.data.data
                    isLoading.value = false
                })
                return{
                   newsList, isLoading
                }
            }
        })

        // 全局注册自定义加载指令
        app.directive('loading',{
            mounted(el,binding){    // 元素渲染加载的时候，执行
                el.classList.add('loading')
            },
            updated(el,binding){    // 元素更改的时候，执行
                binding.value ? el.classList.add('loading') : el.classList.remove("loading")
            }
        })
        app.mount("#app")
    </script>
</body>

</html>